package org.toonWatch

import org.springframework.dao.DataIntegrityViolationException
import org.codehaus.groovy.grails.commons.ApplicationHolder as AH
import org.codehaus.groovy.grails.plugins.springsecurity.NullSaltSource
import org.codehaus.groovy.grails.plugins.springsecurity.SpringSecurityUtils
import org.codehaus.groovy.grails.plugins.springsecurity.ui.RegistrationCode

class UserController extends grails.plugins.springsecurity.ui.UserController {
    def saltSource
    def springSecurityService
    
    def userProfile = {
        def user = springSecurityService.currentUser
        return [user: user]
    }
    
    def passwordEdit = {
        def user = springSecurityService.currentUser
        
        def registrationCode = new RegistrationCode(username: user.username).save()
        return [user: user, token: registrationCode.token]
    }
    
    def update2 = {
        def user = findById()
        if (!user) return
        if (!versionCheck('user.label', 'User', user, [user: user])) {
            return
        }
        user.properties = params

        if (!user.save()) {
            render view: 'userProfile', model: buildUserModel(user)
            return
        }

        lookupUserRoleClass().removeAll user
        addRoles user
        userCache.removeUserFromCache user.username
        flash.message = "${message(code: 'default.updated.message', args: [message(code: 'user.label', default: 'User'), user.id])}"
        redirect action: userProfile
    }
}     